#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <string>
using namespace std;

#define NUM 3

pthread_cond_t cond;
pthread_mutex_t mtx;

void* control(void* args)
{
    string s = (char*)args;
    while(true)
    {
	cout << s << " is controling worker to work..." << endl;	
	//pthread_cond_signal(&cond);
	pthread_cond_broadcast(&cond);
	sleep(3);
    }
}

void* work(void* args)
{
    int num = *(int*)args;
    while(true)
    {	
	pthread_cond_wait(&cond, &mtx);
	cout << "我是[" << num << "] is working..." << endl;
    }
}

int main()
{
    pthread_t boss;
    pthread_t worker[NUM];

    pthread_mutex_init(&mtx, nullptr);
    pthread_cond_init(&cond, nullptr);

    pthread_create(&boss, nullptr, control, (void*)"boss");
    for(int i = 0;i < NUM;++i)
    {
	int* num = new int(i);
	pthread_create(worker, nullptr, work, (void*)num);
    }
    pthread_join(boss, nullptr);
    for(int i = 0;i < NUM;++i)
    {
	pthread_join(worker[i], nullptr);
    }
   
    pthread_mutex_destroy(&mtx);
    pthread_cond_destroy(&cond);

    return 0;
}
